package com.xiaoKe.zhonghzu.ui.fzxz;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat;
import com.bin.david.form.data.format.bg.ICellBackgroundFormat;
import com.bin.david.form.data.table.TableData;
import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.databinding.FragmentDtjBinding;
import com.xiaoKe.zhonghzu.global.base.BaseFragment;
import com.xiaoKe.zhonghzu.global.base.VeinRouter;
import com.xiaoKe.zhonghzu.global.bean.ClFzData;
import com.xiaoKe.zhonghzu.global.bean.FzDataListBean;
import com.xiaoKe.zhonghzu.global.bean.FzDataObject;
import com.xiaoKe.zhonghzu.global.okhttp.ApiCallback;
import com.xiaoKe.zhonghzu.global.okhttp.ApiCallbackOnResponse;
import com.xiaoKe.zhonghzu.global.okhttp.ApiService;
import com.xiaoKe.zhonghzu.global.okhttp.RetrofitClient;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;
import com.xiaoKe.zhonghzu.ui.onClick.NoDoubleClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName : zhongzhu
 * @Author : Shijingna
 * @Time : 2025/5/22 15:09
 * @Description : 文件描述
 */
public class DtjFragment extends BaseFragment<FragmentDtjBinding> {
    List<ClFzData> list;

    @Override
    protected void initView() {

    }

    @Override
    public void onResume() {
        super.onResume();
        setData();
    }

    public void setData() {
        list = SPUtils.getDataList("DtjFragment", ClFzData.class);
        if (null == list || list.size() == 0) {
            getView().findViewById(R.id.line_zwsj).setVisibility(View.VISIBLE);
            getView().findViewById(R.id.table).setVisibility(View.GONE);
            getView().findViewById(R.id.line).setVisibility(View.GONE);
        } else {
            ICellBackgroundFormat<CellInfo> backgroundFormat = new BaseCellBackgroundFormat<CellInfo>() {
                @Override
                public int getBackGroundColor(CellInfo cellInfo) {
                    if (cellInfo.row % 2 != 0) {
                        return ContextCompat.getColor(getContext(), R.color.colorBgF8);
                    }
                    return TableConfig.INVALID_COLOR;
                }

                @Override
                public int getTextColor(CellInfo cellInfo) {
                    if (cellInfo.col == 0) {
                        return ContextCompat.getColor(getContext(), R.color.colorMain);
                    }
                    return TableConfig.INVALID_COLOR;
                }
            };

            binding.table.getConfig().setContentCellBackgroundFormat(backgroundFormat);
            binding.table.setData(list);


            // 获取“猪只编号”列（假设列 ID 为 0，需根据实际列顺序调整）
            binding.table.getTableData().setOnItemClickListener(new TableData.OnItemClickListener() {
                @Override
                public void onClick(Column column, String value, Object o, int col, int row) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("data", list.get(row));
                    toFragmentPage(VeinRouter.DTJDETAILSFRAGMENT.setBundle(bundle));
                }
            });
        }

    }

    @Override
    protected void initData() {
        NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View view) {
                switch (view.getId()) {
                    case R.id.delete:
                        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                        builder.setTitle("提示")
                                .setMessage("确定删除所有数据吗？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        SPUtils.putDataList("DtjFragment", new ArrayList<>());
                                        getActivity().finish();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 处理取消按钮的点击事件
                                    }
                                })
                                .setCancelable(false) // 设置对话框不可取消
                                .show(); // 显示对话框
                        break;
                    case R.id.add:
                        RetrofitClient.getClient().create(ApiService.class).postFzData("繁殖性状-新增", list)
                                .enqueue(new ApiCallback(new ApiCallbackOnResponse() {
                                    @Override
                                    public void onResponse(Object v) {
                                        FzDataObject fzDataListBean = (FzDataObject) v;
                                        if (null != fzDataListBean && fzDataListBean.isSucceeded()) {
                                            SPUtils.putDataList("DtjFragment", new ArrayList<>());
                                            getActivity().finish();
                                        }
                                    }
                                }));
                        break;
                }
            }
        };
        binding.delete.setOnClickListener(noDoubleClickListener);
        binding.add.setOnClickListener(noDoubleClickListener);
    }
}
